package hlhj.fhp.supreme.fragment

import android.animation.ObjectAnimator
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import com.bumptech.glide.Glide
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import ezy.ui.view.BannerView
import hlhj.fhp.supreme.MainActivity
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.*
import hlhj.fhp.supreme.adapters.HonePagerAdp
import hlhj.fhp.supreme.customview.JiePanPayDialog
import hlhj.fhp.supreme.javabean.*
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.home_fgm.*
import kotlinx.android.synthetic.main.home_fgm.view.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode
import java.util.ArrayList

/**
 * Created by Administrator on 2018\3\6 0006.
 */
class HomeFgm:Fragment() {
    private lateinit var rootView:View
    private lateinit var pagerAdp :HonePagerAdp
    private val pagerDatas=ArrayList<BannerBean.DataBean>()
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView= inflater.inflate(R.layout.home_fgm,null)
        initVIew()
        initListener()
        return rootView
    }

    override fun onDestroy() {
        super.onDestroy()
    }
    private fun initVIew() {
//        rootView.viewPager.offscreenPageLimit=5
        rootView.btJiePan.setOnClickListener {
            if (MainActivity.isHxOk){
                context.startActivity(Intent(context,JiePanAty::class.java))
            }else{
                MyUtils.toast("等待解盘登录中，请稍后再试")
            }
        }
        /*轮播*/
        OkGo.post<BannerBean>(Urls.BaseUrl+Urls.BANNER)
                .execute(object :JsonCallBack<BannerBean>(activity){
                    override fun onSuccess(response: Response<BannerBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){

                            val bView = rootView.findViewById<BannerView<BannerBean.DataBean>>(R.id.bannerView) as BannerView<BannerBean.DataBean>
                            bView.setViewFactory { item, position, container ->
                                val imageView = ImageView(context)
                                imageView.scaleType=ImageView.ScaleType.FIT_XY
                                val dataBean = item as BannerBean.DataBean
                                Glide.with(context).load(dataBean.image).asBitmap().into(imageView)
                                imageView
                            }
                            bView.setDataList(bean?.data)
                            bView.start()
                        }else{
                            MyUtils.toast(bean?.msg)
                        }
                    }
                })
//        提示
        OkGo.post<PhoneBean>(Urls.BaseUrl+Urls.PHONE)
                .params("type","risk_trip")
                .execute(object :JsonCallBack<PhoneBean>(activity){
                    override fun onSuccess(response: Response<PhoneBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
//                            rootView.tv1.setText("是")
                            rootView.marView3.mContent=bean?.data.risk_trip
//                            rootView.marView3.mContent="意大利11111"
                        }
                    }
                })
//        公告
        OkGo.post<TextBean>(Urls.BaseUrl+Urls.TEXT)
                .params("cid",24)
                .execute(object :JsonCallBack<TextBean>(activity){
                    override fun onSuccess(response: Response<TextBean>?) {
                        val bean = response?.body()
                        if (bean?.code==1){
                            rootView.btText.mContent=bean?.data[0].post_title
//                            rootView.bottomNotice.setText(bean?.data[0].post_title)
                        }
                    }
                })
    }

    private fun initListener() {
        rootView.loClass.setOnClickListener {
            context.startActivity(Intent(context,ClassAty::class.java))
        }
        rootView.btRead.setOnClickListener {
            context.startActivity(Intent(context,ReadAty::class.java))
        }

        rootView.btTecMaterial.setOnClickListener {
            context.startActivity(Intent(context,TecMaterialAty::class.java))

        }
        rootView.btOption.setOnClickListener {
            context.startActivity(Intent(context,OptionAty::class.java))

        }
        rootView.btAppointment.setOnClickListener {
            context.startActivity(Intent(context,AppointmentAty::class.java))
            /*OkGo.post<AccountBean>(Urls.BaseUrl+Urls.ACCOUNTINFO)
                    .execute(object :JsonCallBack<AccountBean>(activity){
                        override fun onSuccess(response: Response<AccountBean>?) {
                            val bean = response?.body()
                            if (bean?.code==1){
                                if (bean?.data.name.isEmpty()||bean?.data.name==null){
                                    context.startActivity(Intent(context,AppointmentAty::class.java))
                                }else{
                                    if (bean?.data.status==0){
                                        context.startActivity(Intent(context,TipsAty::class.java))
                                    }else if (bean?.data.status==1){
                                        MyUtils.toast("您已经开过户了")
                                    }
                                }
                            }else{
                            }
                        }
                    })*/
        }

    }
}